/**
 * 
 * ***/
package {
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;

	public class Preloader extends MovieClip {

		private var loadBar:MovieClip = new AssetLoader;

		public function Preloader() {
			super();
			
			loadBar.gotoAndPlay(1);
			loadBar.x = stage.stageWidth/2;
			loadBar.y = stage.stageHeight/2;
			addChild(loadBar);
			
			this.addEventListener(Event.ENTER_FRAME, updateFrame);
		}
		
		private function updateFrame(e:Event):void{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = StageQuality.BEST;

			loadBar.x = this.stage.stageWidth/2;
			loadBar.y = this.stage.stageHeight/2;

			var factor:Number = new Number(this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal);
			if (this.currentFrame == this.totalFrames){
				this.removeEventListener(Event.ENTER_FRAME, updateFrame);
				startup();
			}
			var fact:Number = new Number(int(140*(this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)));
			// loadBar.percent.text = int(100*(fact/140));
			loadBar.gotoAndStop(fact);
		}
		
		private function startup():void{
			this.stop();
			removeChild(loadBar);
			
			var main:Class = Class(getDefinitionByName('valentines')) as Class;
			stage.addChild(new main() as DisplayObject);
			stage.removeChild(this);
		}
		
	}

}